import java.util.Arrays;

/**
 * @Description: 删列造序
 * @Date:Create in 19:25  2022/5/12
 */
public class minDeletionSize0944 {
    public static void main(String[] args) {
        String[] strs = new String[]{"zyx", "wvu", "tsr"};
        int sum = minDeletionSize2(strs);
        System.out.println(sum);
//        System.out.println(Arrays.toString(arr[0]));
    }


    public static int minDeletionSize1(String[] strs) {
        int a = strs.length;
        int b = strs[0].length();
        int[][] arr = new int[b][a];
        int sum = 0;

        //将网格要比较的字符转换为asscii码，并且放入数组中
        for (int i = 0; i < a; i++) {
            for (int j = 0; j < b; j++) {
                char temp = strs[i].charAt(j);
                int temp_Asscii = Integer.valueOf(temp);
                arr[j][i] = temp_Asscii;
            }
        }


        //比较arr元素中的顺序
        for (int i = 0; i < b; i++) {
            for (int j = 0; j < a; j++) {
                if (j == 0) {
                    continue;
                } else if (arr[i][j] < arr[i][j - 1]) {
                    sum++;
                    break;
                }
            }
        }
        return sum;
    }

    public static int minDeletionSize2(String[] strs) {
        int a = strs.length;
        int b = strs[0].length();
        int sum = 0;
        out:
        for (int i = 0; i < b; i++) {
            for (int j = 0, cur = -1; j < a; j++) {
                int t = (int) strs[j].charAt(i);
                if (t < cur && ++sum >= 0) {
                    continue out;
                }
                cur = t;
            }
        }
        return sum;
    }
}
